package com.dailyblue.java.spring.boot.boot02.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dailyblue.java.spring.boot.boot02.bean.Emp;
import com.dailyblue.java.spring.boot.boot02.bean.JsonResult;
import com.dailyblue.java.spring.boot.boot02.service.EmpService;
import com.dailyblue.java.spring.boot.boot02.util.ResultTool;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmpController {
    @Resource
    private EmpService empService;

    @GetMapping
    public JsonResult<List<Emp>> find() {
        // MyBatisPlus中已经实现的全查询操作
        List<Emp> list = empService.list();
        return ResultTool.success(list);
    }

    @PostMapping
    public void save(Emp emp) {
        empService.saveOrUpdate(emp);
    }

    @PutMapping
    public void update(Emp emp) {
        empService.saveOrUpdate(emp);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") int id) {
        empService.removeById(id);
    }

    // 查询这个职位的所有员工
    @GetMapping("/{job}")
    public JsonResult<List<Emp>> find(@PathVariable("job") String job) {
        // 产生条件对象
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        // 设置条件 eq(=) ne(<>) le(<=) lt(<) ge(>=) gt(>)
        wrapper.eq("job", job); // job=#{job}
        List<Emp> list = empService.list(wrapper);
        return ResultTool.success(list);
    }

    // 模糊查询和多条件and查询
    @GetMapping("/{ename}/{job}")
    public JsonResult<List<Emp>> find(@PathVariable("ename") String ename, @PathVariable("job") String job) {
        // 产生条件对象
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.eq("job", job);
        // like(%e%) likeLeft(%e) likeRight(e%) notLike(not like %e%)
        wrapper.like("ename", ename);
        List<Emp> list = empService.list(wrapper);
        return ResultTool.success(list);
    }

    // 引入了or
    @GetMapping("/2/{ename}/{job}")
    public JsonResult<List<Emp>> find2(@PathVariable("ename") String ename, @PathVariable("job") String job) {
        // 产生条件对象
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.eq("job", job).or();
        // like(%e%) likeLeft(%e) likeRight(e%) notLike(not like %e%)
        wrapper.like("ename", ename);
        List<Emp> list = empService.list(wrapper);
        return ResultTool.success(list);
    }

    @GetMapping("/3/{sal1}/{sal2}")
    public JsonResult<List<Emp>> find3(@PathVariable("sal1") int sal1, @PathVariable("sal2") int sal2) {
        // 产生条件对象
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        // between and  not between and
        wrapper.between("sal", sal1, sal2);
        List<Emp> list = empService.list(wrapper);
        return ResultTool.success(list);
    }

    @GetMapping("/4/{column}")
    public JsonResult<List<Emp>> find5(@PathVariable("column") String column) {
        // 产生条件对象
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.isNull(column);
        List<Emp> list = empService.list(wrapper);
        return ResultTool.success(list);
    }

    @GetMapping("/5")
    public JsonResult<List<Emp>> find6(String... jobs) {
        // 产生条件对象
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        // in not in
        wrapper.in("job", jobs);
        List<Emp> list = empService.list(wrapper);
        return ResultTool.success(list);
    }
}
